/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhongketech.sunflower.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.domain.User;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;


public class UserDao {
	public static final String TABLE_NAME = "uers";
	public static final String COLUMN_NAME_ID = "username";
	public static final String COLUMN_NAME_NICK = "nick";
	

	private DbOpenHelper dbHelper;

	public UserDao(Context context) {
		dbHelper = DbOpenHelper.getInstance(context);
	}

	/**
	 * 保存好友list
	 * 
	 * @param contactList
	 */
	public void saveContactList(List<User> contactList) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if (db.isOpen()) {
			db.delete(TABLE_NAME, null, null);
			for (User user : contactList) {
				ContentValues values = new ContentValues();
				values.put(COLUMN_NAME_ID, user.getUsername());
				if(user.getNick() != null)
					values.put(COLUMN_NAME_NICK, (String) user.getNick());
				db.insert(TABLE_NAME, null, values);
			}
		}
	}

	/**
	 * 获取好友list
	 * 
	 * @return
	 */
	public Map<String, User> getContactList() {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Map<String, User> users = new HashMap<String, User>();
//		if (db.isOpen()) {
//			Cursor cursor = db.rawQuery("select * from " + TABLE_NAME /* + " desc" */, null);
//			while (cursor.moveToNext()) {
//				String username = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_ID));
//				String nick = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_NICK));
//				User user = new User();
//				user.setUsername(username);
//				user.setNick(nick);
//				String headerName = null;
//				if (!TextUtils.isEmpty(user.getNick())) {
//					headerName = (String) user.getNick();
//				} else {
//					headerName = user.getUsername();
//				}
//				
//				if (username.equals(Constant.NEW_FRIENDS_USERNAME) || username.equals(Constant.GROUP_USERNAME)) {
//					user.setHeader("");
//				} else if (Character.isDigit(headerName.charAt(0))) {
//					user.setHeader("#");
//				} else {
//					user.setHeader(HanziToPinyin.getInstance().get(headerName.substring(0, 1))
//							.get(0).target.substring(0, 1).toUpperCase());
//					char header = user.getHeader().toLowerCase().charAt(0);
//					if (header < 'a' || header > 'z') {
//						user.setHeader("#");
//					}
//				}
//				users.put(username, user);
//			}
//			cursor.close();
//		}
		return users;
	}
	
	/**
	 * 删除一个联系人
	 * @param username
	 */
	public void deleteContact(String username){
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if(db.isOpen()){
			db.delete(TABLE_NAME, COLUMN_NAME_ID + " = ?", new String[]{username});
		}
	}
	
	
	/**
	 * 保存一个联系人
	 * @param user
	 */
	public void saveContact(User user){
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(COLUMN_NAME_ID, user.getUsername());
		if(user.getNick() != null)
			values.put(COLUMN_NAME_NICK, user.getNick());
		if(db.isOpen()){
			db.insert(TABLE_NAME, null, values);
		}
	}
	
	

}
